%Goal: mortgage market equilibrium




function [rno, sno, rnbl,snbl,rnb,snb,rm,sbm,rbl,sbl] = Solve_profit_wbank(psim,psiw,alpha,k,Nb,Nn,Nm,qn,qb,eta,rhoo,rhob,rhobl,sow,sbw,rb)

MaxIterations=10000;
tol = 10^(-9);
rbl_initial = 1.81;
rbl = [];
diff = 1000;
iter = 0;

while diff>tol && iter<=MaxIterations  
        iter = iter+1;
        [rno, sno, rnbl,snbl,rnb,snb,rm,sbm] = Solve_profit_mbank(psim,psiw,alpha,k,Nb,Nn,Nm,qn,qb,eta,rhoo,rhob,rhobl,sow,sbw,rb,rbl_initial);
        sbl = exp(-alpha*rbl_initial+qb)/(1+exp(-alpha*rbl_initial+qb)+(Nb-1)*exp(-alpha*rb+qb)+Nm*exp(-alpha*rm+qb)+Nn*sow*exp(-alpha*rno+qn)+Nn*sbw*exp(-alpha*rnbl+qn)+Nn*sbw*(Nb-1)*exp(-alpha*rnb+qn));
        
        rbl = psim+1/(alpha*(1-sbl))+eta*(rhobl-psiw)*Nn*sbw*snbl/(1-sbl);
        diff = abs(rbl-rbl_initial);
        rbl_initial = rbl;    
  
end 
end





